Student Solution

-->

"Education is the most powerful weapon which you can use to change the world”
– Nelson Mandela

1 University

1 Course

2 Subjects

4.4 - Creating an R Function

4.4 - Creating an R Function

Q Create an R Function In this activity, you will need to review the formula for finding the distance between two points on a Cartesian coordinate system. You can visit the Purplemath website to learn how to calculate The Distance Formula.Links to an external site. Create an R function that calculates the distance between two points (x1,y1) and (x2,y2). The function will have four arguments that represent the x- and y-values of the two points and return the distance between the two points. Execute your function for the following four sets of points: 1. (2,3) and (13,8) 2. (?4,?1) and (7,?3) 3. (73,15) and (13,65) 4. (23,6) and (?3,56) Note: Do not change the fractions nor the square roots to decimal numbers for use in the function arguments. Enter the arguments as fractions and square roots. You may want to investigate how to enter a square root in R. Submit a document containing your R code and a screenshot of the execution and results for the four sets of points. ________________________________________ Save your assignments using a naming convention that includes your first and last name and the activity number (or description). Do not add punctuation or special characters.

View Related Questions

Solution Preview

Create an R function that calculates the distance between two points: dist <-function(n){ (x1,y1)(x1,y1) <- (2,3)(2,3) (x2,y2)(x2,y2) <- (13,8)(13,8) dist <- sqrt[sqrt((x1,y1(x1,y1))-(x2,y2)(x2,y2)) + sqrt((x1,y1(x1,y1))-(x2,y2)(x2,y2))] }